package leetcode;

/*
504. 七进制数
给定一个整数，将其转化为7进制，并以字符串形式输出。
示例 1:
输入: 100
输出: "202"
示例 2:
输入: -7
输出: "-10"
注意: 输入范围是 [-1e7, 1e7] 。
*/

public class problems_504 {
    public static void main(String[] args) {
        System.out.println(new Solution().convertToBase7(100));
    }

    static class Solution {
        public String convertToBase7(int num) {
            if(num == 0) return "0";
            boolean minus = false;
            if (num < 0){
                minus = true;
                num = -num;
            }

            StringBuffer ret = new StringBuffer();
            int add = 0;
            while (num > 0){
                int divisor = (num + add) % 7;
                num = (num + add) / 7;
                ret.append(divisor);
            }
            if(add > 0){
                ret.append(add);
            }
            return (minus ? "-" : "") + ret.reverse().toString();
        }
    }
}